<?php
$title = isset($action) && $action=='update'?"Cập nhật":"Thêm mới";
$isOwner = ($this->session->userdata('image_folder')==$oPost->image_folder)?true:false;

$lastUrl = "";
if(isset($_SERVER['HTTP_REFERER'])){
    if($_SERVER['HTTP_REFERER'] == admin_post_url($oPost->getSlug())){
        $lastUrl = admin_page_url(POST_SLUG);
    } else {
        $lastUrl = $_SERVER['HTTP_REFERER'];
    }
} else {
    $lastUrl = admin_page_url(POST_SLUG);
}
?>

<div class="row">
    <h1 class="col-md-6"><?php echo $title; ?> <small>Quản lý các tin tức</small></h1>

    <h1 class="col-md-6">
        <a class="btn btn-default pull-right mgl5" href="<?php echo $lastUrl; ?>">
            <i class="glyphicon glyphicon-share-alt"></i> Trở về
        </a>
        <?php if(isset($action) && $action=='update') { ?>
        <input type="hidden" name="post[id]" value="<?php echo $oPost->getId(); ?>" />
        <a target="_blank" class="btn btn-success pull-right mgl5" href="<?php echo post_single_url($oPost->getSlug()); ?>"><i class="fa fa-search"></i> Xem</a>
        <button type="button" class="btn btn-success pull-right" onclick="wireOfAction('#btn-submit-post', 'click');"><i class="glyphicon glyphicon-ok-sign"></i> Cập nhật</button>
        <?php } else { ?>
        <button type="button" class="btn btn-success pull-right"><i class="glyphicon glyphicon-plus-sign"></i> Thêm mới</button>
        <?php } ?>
    </h1>
</div>

<ol class="breadcrumb">
    <li><a href="<?php echo admin_page_url(); ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
    <li><a href="<?php echo admin_page_url(POST_SLUG); ?>"><i class="fa fa-table"></i> Tin tức</a></li>
    <li class="active"><i class="fa fa-file"></i> <?php echo $oPost->getTitle(); ?></li>
</ol>

<form role="form" method="post" action="<?php echo $action_link; ?>" data-callback="1">
    <div class='row' id="panel-new-post">
        <div class="col-md-5 col-xs-12">
            <div class="form-group">
                <div class="row">
                    <div class="media has-change-img-control">
                        <a title="<?php echo $oPost->getTitle(); ?>" id="post-img" class="thumbnail fancybox" rel="group"
                            href="<?php echo $oPost->getImage()!=null ? post_upload($oPost->getImage(), $oPost->image_folder, false): '#'; ?>">
                            <?php if($oPost->getImage()!='') { ?>
                            <img class="media-object" src="<?php echo post_upload($oPost->getImage(), $oPost->image_folder); ?>" />
                            <?php } else { ?>
                            <img data-src="holder.js/100%x180" class="media-object" />
                            <?php } ?>
                        </a>

                        <?php if($this->session->userdata('image_folder')==$oPost->image_folder) { ?>
                        <div class="img-control">
                            <a class="btn btn-success" href="#" data-toggle="modal" data-target="#modal-change-img">
                                <i class="glyphicon glyphicon-pencil"></i>
                                Thay đổi
                            </a>
                        </div>
                        <?php } ?>

                    </div>
                    <!-- end image -->

                    <div class="form-group col-md-12">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" id="input-post-order" name="post[publish_flg]" value="1" <?php echo $oPost->getPublishFlg()==1?'checked="checked"':''; ?> />
                                Hiển thị
                            </label>
                        </div>
                    </div>

                    <div class="form-group col-md-12">
                        <label for="input-post-title">Tên</label>
                        <input type="text" class="form-control auto-slug" follow-ctr='#input-post-slug' id="input-post-title" name="post[title]" value="<?php echo $oPost->getTitle(); ?>" />
                    </div>

                    <div class="form-group col-md-12">
                        <label for="input-post-template">Đường dẫn</label>
                        <input type="text" class="form-control" id="input-post-slug" name="post[slug]" value="<?php echo $oPost->getSlug(); ?>"/>
                    </div>

                    <div class="form-group col-md-6">
                        <label for="input-post-template">Danh mục</label>
                        <select class="form-control" name="post[post_cate_id]">
                            <?php foreach($aPostCate as $cate) {?>
                            <option value="<?php echo $cate->getId(); ?>" <?php echo $cate->getId()==$oPost->getPostCateId()?'selected="selected"':''; ?>>
                                <?php echo $cate->getTitle(); ?>
                            </option>
                            <?php } ?>
                        </select>
                    </div>

                    <div class="form-group col-md-6">
                        <label for="input-post-order">Thứ tự</label>
                        <input type="text" class="form-control" id="input-post-order" name="post[order]" value="<?php echo $oPost->getOrder(); ?>" />
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>

        <div class="col-md-7 col-xs-12">
            <div class="form-group">
                <label for="input-post-order">Mô tả</label>
                <textarea class="form-control" name="post[description]" rows="5"><?php echo $oPost->getDescription(); ?></textarea>
            </div>
            <div class="form-group">
                <label for="input-post-order">Nội dung</label>
                <textarea class="ckeditor" name="post[content]" rows="100"><?php echo $oPost->getContent(); ?></textarea>
            </div>
            <div class="form-group">
                <input type="hidden" name="post[image_folder]" value="<?php echo $this->encrypt->encode($oPost->image_folder); ?>" />
                <?php if($isOwner) { ?>
                <input type="hidden" name="post[image]" id="input-post-img" value="<?php echo POST_IMAGE_URL.$oPost->getImage(); ?>" />
                <?php } ?>
                <?php if(isset($action) && $action=='update') { ?>
                <input type="hidden" name="post[id]" value="<?php echo $this->encrypt->encode($oPost->getId()); ?>" />
                <button type="button" id="btn-submit-post" class="btn btn-success pull-left"><i class="glyphicon glyphicon-ok-sign"></i> Cập nhật</button>
                <?php } else { ?>
                <button type="button" id="btn-submit-post" class="btn btn-success pull-left"><i class="glyphicon glyphicon-plus-sign"></i> Thêm mới</button>
                <?php } ?>
                <a class="btn btn-default pull-left mgl5" href="<?php echo $lastUrl; ?>">
                    <i class="glyphicon glyphicon-share-alt"></i> Trở về
                </a>
            </div>
        </div>
    </div>
</form>

<?php if($isOwner) { $this->load->view('backend/admin/image-modal/post.html.php'); } ?>